const Service = require('egg').Service;
let uuid = require('node-uuid')
class ProductService extends Service {
  async getDetail(id) {
    try{
      let list = await this.app.mysql.select('product',{
        where:{pid:id}
      })
      if(list.length > 0){
        return{
          code:1,
          list
        }
      }
    }catch(e){
      return{
        code:0,
        msg:e.message
      }
    }
  }
  async addroduct(data){
    let orderid = uuid.v1();
    let timer = new Date()
    try{
      console.log(data)
      data.list.forEach((item)=>{
         this.app.mysql.insert('orderlist',{
          title:item.title,
          money:item.num*item.price,
          num:item.num,
          user:data.username,
          create_timer:timer,
          orderid:orderid
        })
      })
      return{
        code:1,
        msg:"提交订单成功"
      }
    }catch(e){
      return{
        code:0,
        msg:e.message
      }
    }
  }
}

module.exports = ProductService;